package com.laxcen.toydemo;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.apkfuns.logutils.LogUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import util.AppActivityManager;
import util.StrUtil;
import util.TagResult;
import util.eventbus.MessageEvent;
import util.eventbus.MessageEventType;

public class BaseActivity extends AppCompatActivity {

    public AppActivityManager appActivityManager = AppActivityManager.getInstance();

    public AppActivityManager getAppActivityManager() {
        return appActivityManager;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        appActivityManager.addActivity(this);
        LogUtils.d(BaseActivity.this.getClass().getName() + "--------onCreate");
        setWindowFullScreen();
        EventBus.getDefault().register(this);
    }

    /*
     * 这个函数必须在view加载前执行*/
    public void setWindowFullScreen() {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    public void startActivity(Class<?> cls) {
        Intent newIntent = new Intent(this, cls);
        startActivity(newIntent);
    }

    public void startActivityAndFinish(Class<?> cls) {
        Intent newIntent = new Intent(this, cls);
        startActivity(newIntent);
        finish();
    }

    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(this, cls);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        LogUtils.d(BaseActivity.this.getClass().getName() + "--------onCreate");
    }

    @Override
    protected void onStart() {
        super.onStart();
        LogUtils.d(BaseActivity.this.getClass().getName() + "--------onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        LogUtils.d(BaseActivity.this.getClass().getName() + "--------onResume");
    }

    @Override
    protected void onStop() {
        super.onStop();
        LogUtils.d(BaseActivity.this.getClass().getName() + "--------onStop");
    }

    @Override
    protected void onPause() {
        super.onPause();
        LogUtils.d(BaseActivity.this.getClass().getName() + "--------onPause");
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        LogUtils.d(BaseActivity.this.getClass().getName() + "--------onNewIntent");
    }

    protected boolean hasLeftSlidingMenu() {
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        appActivityManager.finishActivity(this);
        LogUtils.d(BaseActivity.this.getClass().getName() + "--------onDestory");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        onMessageEventProcess(event);
    }

    protected static long currentTagsLastChangedTime = System.currentTimeMillis();
    protected static String currentTagsStr = "";
    protected static List<String> currentTags = null;

    protected void onMessageEventProcess(MessageEvent event) {
        if (event.getType() == MessageEventType.BLE.FoundTags) {
            List<String> tags = (List<String>) event.getParameter();
            String tagsStr = StrUtil.arrayToString(tags);
            LogUtils.d("onMessageEventProcess " + tagsStr);
            currentTags = tags;
            if (!currentTagsStr.equals(tagsStr)) {
                currentTagsStr = tagsStr;
                currentTagsLastChangedTime = System.currentTimeMillis();
            }
        }
    }

    protected static final int LoopDetectionMillis = 1000;

    protected boolean checkOption(List<String> tags, String tag) {
        boolean hasTag = false;
        long diff = System.currentTimeMillis() - currentTagsLastChangedTime;
        if (diff > 1300) {
            //认为tag一直没变化，就算是没有放标签
            return hasTag;
        }
        for (String item : tags) {
            if (!StrUtil.isBlank(item) && !item.equals(tag)) {
                hasTag = false;
                break;
            }
            if (item.equals(tag)) {
                hasTag = true;
            }
        }
        return hasTag;
    }

    protected boolean checkHasAnyTag(List<String> tags) {
        boolean hasTag = false;
        long diff = System.currentTimeMillis() - currentTagsLastChangedTime;
        if (diff > 1300) {
            //认为tag一直没变化，就算是没有放标签
            return hasTag;
        }
        for (String item : tags) {
            if (!StrUtil.isBlank(item)) {
                hasTag = true;
                break;
            }
        }
        return hasTag;
    }

    protected boolean checkMatchStringTag(List<String> tags, String matchStr) {
        boolean hasTag = false;
        long diff = System.currentTimeMillis() - currentTagsLastChangedTime;
        if (diff > 1300) {
            //认为tag一直没变化，就算是没有放标签
            return hasTag;
        }
        StringBuilder sb = new StringBuilder();
        for (String item : tags) {
            if (!StrUtil.isBlank(item)) {
                sb.append(item);
            }
        }
        if (sb.toString().equals(matchStr)) hasTag = true;
        return hasTag;
    }

    protected TagResult checkHasString(List<String> tags) {
        TagResult tagResult = new TagResult();
        long diff = System.currentTimeMillis() - currentTagsLastChangedTime;
        if (diff > 1300) {
            //认为tag一直没变化，就算是没有放标签
            tagResult.setHasString(false);
            return tagResult;
        }
        StringBuilder sb = new StringBuilder();
        for (String item : tags) {
            if (!StrUtil.isBlank(item)) {
                sb.append(item);
            }
        }
        String str = sb.toString();
        tagResult.setHasString(!StrUtil.isBlank(str));
        tagResult.setTagString(str);
        return tagResult;
    }

}
